package com.rambo.baselib.user.slice;

import com.amap.adapter.text.TextUtils;
import com.rambo.baselib.BaseAbilitySlice;
import com.rambo.baselib.ResourceTable;
import com.rambo.baselib.constant.ParamConstant;
import com.rambo.baselib.msgbus.EventBus;
import com.rambo.baselib.user.RegisterAbility;
import com.rambo.baselib.util.PrefUtil;
import com.rambo.baselib.util.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.OnClickListener;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;

public class LoginAbilitySlice extends BaseAbilitySlice {

    Button btnLogin;
    TextField tfAccount, tfPwd;
    Text tvRegister;

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_login;
    }

    @Override
    protected void initView() {
        super.initView();
        btnLogin = (Button) findComponentById(ResourceTable.Id_btn_login);
        tfAccount = (TextField) findComponentById(ResourceTable.Id_tf_account);
        tfPwd = (TextField) findComponentById(ResourceTable.Id_tf_pwd);
        tvRegister = (Text) findComponentById(ResourceTable.Id_tv_register);

        toolbarHolder.setDefaultTitle("登录");
    }

    @Override
    protected void registerClick() {
        super.registerClick();
        tvRegister.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent.OperationBuilder operationBuilder = new Intent.OperationBuilder()
                        .withBundleName(getBundleName())
                        .withAbilityName(RegisterAbility.class.getName())
                        .withDeviceId("");
                Intent intent = new Intent();
                intent.setOperation(operationBuilder.build());
                startAbility(intent);
            }
        });
        btnLogin.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                String account = tfAccount.getText().trim().toString();
                String pwd = tfPwd.getText().trim().toString();
                if (TextUtils.isEmpty(account)) {
                    ToastUtil.toast("请输入账号");
                    return;
                }
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.toast("请输入密码");
                    return;
                }
                //TODO 假设这里已经完成登陆操作
                PrefUtil.getInstance().write(ParamConstant.PREF_KEY.USER_IS_LOGIN, true);
                PrefUtil.getInstance().write(ParamConstant.PREF_KEY.USER_NAME, "张三");
                PrefUtil.getInstance().write(ParamConstant.PREF_KEY.USER_ICON, "https://imgsa.baidu.com/forum/w%3D580/sign=2e33aed58713632715edc23ba18ea056/92b7d0a20cf431adf40371304636acaf2fdd98e1.jpg");
                EventBus.getEventBus().publish(ParamConstant.EVENT_TYPE.LOGIN_SUCCESS, Boolean.TRUE);
                onBackPressed();
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
